/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.company.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.company.entity.TpCompany;
import com.thinkgem.jeesite.modules.company.service.TpCompanyService;

/**
 * 管理上报种子企业Controller
 * @author 王虎
 * @version 2017-03-10
 */
@Controller
@RequestMapping(value = "${adminPath}/company/tpCompany")
public class TpCompanyController extends BaseController {

	@Autowired
	private TpCompanyService tpCompanyService;
	
	@ModelAttribute
	public TpCompany get(@RequestParam(required=false) String id) {
		TpCompany entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = tpCompanyService.get(id);
		}
		if (entity == null){
			entity = new TpCompany();
		}
		return entity;
	}
	
	@RequiresPermissions("company:tpCompany:view")
	@RequestMapping(value = {"list", ""})
	public String list(TpCompany tpCompany, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TpCompany> page = tpCompanyService.findPage(new Page<TpCompany>(request, response), tpCompany); 
		model.addAttribute("page", page);
		return "modules/company/tpCompanyList";
	}

	@RequiresPermissions("company:tpCompany:view")
	@RequestMapping(value = "form")
	public String form(TpCompany tpCompany, Model model) {
		model.addAttribute("tpCompany", tpCompany);
		return "modules/company/tpCompanyForm";
	}

	@RequiresPermissions("company:tpCompany:edit")
	@RequestMapping(value = "save")
	public String save(TpCompany tpCompany, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tpCompany)){
			return form(tpCompany, model);
		}
		tpCompanyService.save(tpCompany);
		addMessage(redirectAttributes, "保存种子企业成功");
		return "redirect:"+Global.getAdminPath()+"/company/tpCompany/?repage";
	}
	
	@RequiresPermissions("company:tpCompany:edit")
	@RequestMapping(value = "delete")
	public String delete(TpCompany tpCompany, RedirectAttributes redirectAttributes) {
		tpCompanyService.delete(tpCompany);
		addMessage(redirectAttributes, "删除种子企业成功");
		return "redirect:"+Global.getAdminPath()+"/company/tpCompany/?repage";
	}

}